package com.insta.formation.calendar.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * Un evenement contient : - une date de d�but, et de fin - un nom - un ensemble
 * d'invit�s qui peut lire cet evenements.
 * 
 * @author Julien
 * 
 */
@Entity
@Table(name = "EVENT")
public class Event implements java.io.Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1930062752139831959L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	private String name;

	private Date startDate;

	private Date endDate;

	@ManyToOne
//	@JoinColumn(name = "Creator_FK", insertable = false, updatable = false)
	@JoinColumn(name="Creator_FK", nullable = false)
	private Person creator;

	@ManyToMany(targetEntity = com.insta.formation.calendar.model.Person.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinTable(name = "EVENT_PERSON", joinColumns = @JoinColumn(name = "EVENT_ID"), inverseJoinColumns = @JoinColumn(name = "PERSON_ID"))
	private Set<Person> guests;
	
	@ManyToOne
	@JoinColumn(name = "Calendar_PK", nullable = false)
	private Calendar calendar;

	public Event() {
		super();
		guests = new HashSet<Person>();
	}

	/**
	 * Retourne l'identifiant de l'Event
	 * 
	 * @return - identifiant de l'Event
	 */
	public int getId()
	{
		return id;
	}

	/**
	 * Modifie l'identifiant de l'Event
	 * 
	 * @param id
	 *            - nouveau identifiant de l'Event
	 */
	public void setId(int id)
	{
		this.id = id;
	}

	/**
	 * Retourne le nom de l'Event
	 * 
	 * @return nom de l'Event
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * Modifie le nom de l'Event
	 * 
	 * @param name
	 *            - nom de l'Event
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * Retourne la date de d�but de l'Event
	 * 
	 * @return - date de d�but de l'Event
	 */
	public Date getStartDate()
	{
		return startDate;
	}

	/**
	 * Modifie la date de d�but de l'Event
	 * 
	 * @param startDate
	 *            - date de d�but de l'Event
	 */
	public void setStartDate(Date startDate)
	{
		this.startDate = startDate;
	}

	/**
	 * Retourne la date de fin de l'Event
	 * 
	 * @return - date de fin de l'Event
	 */
	public Date getEndDate()
	{
		return endDate;
	}

	/**
	 * Modifie la date de fin de l'Event
	 * 
	 * @param startDate
	 *            - date de fin de l'Event
	 */
	public void setEndDate(Date endDate)
	{
		this.endDate = endDate;
	}

	/**
	 * Retourne la personne qui a cr�e l'Event
	 * 
	 * @return - personne qui a cr�e l'Event
	 */
	public Person getCreator()
	{
		return creator;
	}

	/**
	 * Modifie la personne qui a cr�e l'Event
	 * 
	 * @param creator
	 *            - personne qui a cr�e l'Event
	 */
	public void setCreator(Person creator)
	{
		this.creator = creator;
	}
	
	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	/**
	 * Retourne les visiteurs de l'Event
	 * 
	 * @return - visiteurs de l'Event
	 */
	public Set<Person> getGuests()
	{
		return guests;
	}

	/**
	 * Modifie les visiteurs de l'Event
	 * 
	 * @param guests
	 *            - visiteurs de l'Event
	 */
	public void setGuests(Set<Person> guests)
	{
		this.guests = guests;
	}

	/**
	 * Ajout un visiteur dans l'Event
	 * 
	 * @param guest
	 *            - nouveau visiteur dans l'Event
	 */
	public void addGuest(Person guest)
	{
		this.guests.add(guest);
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((startDate == null) ? 0 : startDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		Event other = (Event) obj;
		if (creator == null) {
			if (other.creator != null) return false;
		}
		else if (!creator.equals(other.creator)) return false;
		if (endDate == null) {
			if (other.endDate != null) return false;
		}
		else if (!endDate.equals(other.endDate)) return false;
		if (guests == null) {
			if (other.guests != null) return false;
		}
		else if (!guests.equals(other.guests)) return false;
		if (name == null) {
			if (other.name != null) return false;
		}
		else if (!name.equals(other.name)) return false;
		if (startDate == null) {
			if (other.startDate != null) return false;
		}
		else if (!startDate.equals(other.startDate)) return false;
		return true;
	}

	@Override
	public String toString()
	{
		return "Event [id=" + id + ", name=" + name + ", startDate=" + startDate + ", endDate=" + endDate + ", creator=" + creator + ", guests=" + guests + "]";
	}
}
